<?php

namespace app\api\services\user;

use app\api\constant\JoinStatusConst;
use app\api\model\JoinMerchant;
use app\api\model\JoinType;
use app\api\services\BaseServices;
use think\Exception;

class JoinMerchantServices extends BaseServices
{
    /**
     * 商家入驻
     * @param array $params
     * @return void
     * @throws Exception
     */
    public function join(array $params)
    {
        $userMerchant = JoinMerchant::getByUserId($params['user_id']);
        if ($userMerchant && $userMerchant['status'] != JoinStatusConst::SUCCESS) {
            throw new Exception('该用户已经申请入驻，请耐心等待审核结果');
        }
        // 检查类型是否存在
        $joinType = JoinType::getById($params['join_type_id']);
        if (!$joinType) {
            throw new Exception("类型不存在");
        }
        JoinMerchant::modelCreate($params);

    }

    /**
     * 获取入驻信息
     * @param int $userid
     */
    public function getJoinMerchant(int $userid)
    {
        return JoinMerchant::getByUserId($userid)->toArray() ?? [];
    }
}